package com.schedule.task.controller;

import com.alibaba.fastjson.JSON;
import com.schedule.task.constant.Socket;
import com.schedule.task.dao.BookCustomServiceMapper;
import com.schedule.task.entity.BookCustomService;
import com.schedule.task.util.SocketHeader;
import com.schedule.task.vo.DataSend;
import com.schedule.task.vo.SocketResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;


@Slf4j
@RestController
public class WebSocketController {

    private StringRedisTemplate stringRedisTemplate;

    private SimpMessagingTemplate simpMessagingTemplate;

    private BookCustomServiceMapper bookCustomServiceMapper;

    public WebSocketController(StringRedisTemplate stringRedisTemplate, SimpMessagingTemplate simpMessagingTemplate, BookCustomServiceMapper bookCustomServiceMapper) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.simpMessagingTemplate = simpMessagingTemplate;
        this.bookCustomServiceMapper = bookCustomServiceMapper;
    }


    /**
     * 发送消息到客户端
     *
     * @param userId
     */
    public void sendToClient(Long userId) {
        String sessionId = this.stringRedisTemplate.opsForValue().get(Socket.Prefix.USER_PRE_SET + userId);
        log.info("sessionId : {}", sessionId);
        DataSend dataSend = new DataSend();
        dataSend.setSid(1);
        dataSend.setRid(1);
        dataSend.setState(1);
        dataSend.setWords("你好啊! 这是一条信息");
        simpMessagingTemplate.convertAndSendToUser(sessionId, "/getMsg", new SocketResponse<DataSend>(200, "成功", dataSend),
                SocketHeader.createHeaders(sessionId));
    }

    @MessageMapping("/user/{UserId}")
    public void receiveFromUser(DataSend dataSend) {
        log.info("客户端发过来的数据:" + JSON.toJSONString(dataSend));

        BookCustomService bookCustomService = new BookCustomService();
        bookCustomService.setSid(dataSend.getSid());
        bookCustomService.setRid(dataSend.getRid());
        bookCustomService.setState(dataSend.getState());
        bookCustomService.setWords(dataSend.getWords());
        bookCustomService.setImg(dataSend.getImg());
        bookCustomService.setVideo(dataSend.getVideo());
        bookCustomService.setCreateAt(new Date());
        bookCustomService.setStatus(1);
        if (null != dataSend.getRid() && null != dataSend.getSid()) {
            //查询用户是否在线
            //接收者的sessionId
            String sessionIdReceive = this.stringRedisTemplate.opsForValue().get(Socket.Prefix.USER_PRE_SET + dataSend.getRid());
            //发消息的sessionId
            String sessionIdSend = this.stringRedisTemplate.opsForValue().get(Socket.Prefix.USER_PRE_SET + dataSend.getSid());
//            simpMessagingTemplate.convertAndSendToUser(sessionIdSend, "/getMsg", new SocketResponse(200, "留言成功"),
//                    SocketHeader.createHeaders(sessionIdSend));
            if (null != sessionIdReceive) {
                //在线给接收者发送消息
                simpMessagingTemplate.convertAndSendToUser(sessionIdReceive, "/getMsg", new SocketResponse<DataSend>(200, "成功", dataSend));

                //给发送者发送消息已接收
                simpMessagingTemplate.convertAndSendToUser(sessionIdSend, "/getMsg", new SocketResponse(200, "发送成功"),
                        SocketHeader.createHeaders(sessionIdSend));

                //入库
                bookCustomService.setIsRead(1);
                bookCustomServiceMapper.insertSelective(bookCustomService);
            } else {

                //给发送者发送消息已留言
                simpMessagingTemplate.convertAndSendToUser(sessionIdSend, "/getMsg", new SocketResponse(200, "留言成功"),
                        SocketHeader.createHeaders(sessionIdSend));
                //入库
                bookCustomService.setIsRead(-1);
                bookCustomServiceMapper.insertSelective(bookCustomService);
            }
        }
    }

}
